<?php

class Model_ServiceCpxcenter extends Model_Service
{
  
	public function loginCheck($dpt_id, $dpt_account_login, $password="")
	{
		$service_info = $this->getServiceInfo($dpt_id);
			
		$dbconn = $this->getServiceConn($dpt_id);
		//Zend_Debug::dump($dbconn);
		//Zend_Debug::dump($service_info);
		if (isset($dbconn['admin']))
		{
			$select  = $dbconn['admin']->select()->from('user');
			$select->where('login = ?', $dpt_account_login);
			$select->where('network_id = 4');
		
			$user = $dbconn['admin']->fetchRow($select);
			//Zend_Debug::dump($user);
			$passwd = md5($password. $user['salt']);
					
			if (!$user)
			{
				$rs['status'] = false;
				$rs['message'] = 'account not found';
			}
			else
			{
			   	$status_id = $user['status_id'];
         		$passwd_stored = $user['password'];
           
				 if ($status_id !=2)
		     	{
		        	$rs['status'] = false;
            		$rs['message'] = 'account not active';
		     	}
		     	elseif ($password && $passwd !=$passwd_stored)
		    	{
		        	$rs['status'] = false;
            		$rs['message'] = 'wrong password';
		     	}
		     	else
		     	{
		        	$rs['status'] = true;
            		$rs['dpt_account_id'] = $user['user_id'];
           	 		$rs['dpt_account_login'] = $dpt_account_login;
           	 		$rs['email'] = $user['email'];
           	 		
            		if ($user['role'] == 40) $rs['login_area'] = 'admin';
            		else if ($user['role'] == 20) $rs['login_area'] = 'publisher';
            		else if ($user['role'] == 30) $rs['login_area'] = 'advertiser';
            
		     	}    
			}
		}
		else
		{
			$rs['status'] = false;
     		$rs['message'] = 'service connection error';
		}

		return $rs; // return OK if passed
	}
	
	
	public function createAccount($user_id, $dpt_id, $profile)
	{
		$password = Model_User::str_makerand(6,8); //create a random password for remote account
		
		$params=array();
		$params['password']=$password;

		if (is_array($profile)){
			foreach($profile as $key => $value){
				switch($key){
					case 'first_name':
					case 'last_name':
					case 'email':
					case 'company':
					case 'phone':
					case 'address':
					case 'city':
					case 'state':
					case 'zipcode':
					case 'country':
					case 'website':
						if (!empty($value)) $params[ $key ] = $value;
						break;
					default: break;
				}
			}
			
		}
		$params['timezone'] = $this->getTimezone($profile['timezone_id']);
		
		$area = isset($profile['area']) ? $profile['area'] : 'advertiser';  //for account type:advertiser,publisher,admin
		$params['area'] = $area;

		$params['login'] = $profile['email'].'_'.$area;		
		
		$remote_call = 'http://cpxcenter.com/home/server-rpc';
		$client = new Zend_XmlRpc_Client($remote_call);		
		$account_id = $client->call("YesupAcc.addUser",array($params));
		
		if ($account_id>0){
			if ($area=='publisher')
				$login_account = 'CPX-P-'.$account_id;
			else 
				$login_account = 'CPX-A-'.$account_id;
			
			$dpt_login_arr=array();
			$dpt_login_arr['dpt_account_login'] = $login_account;
			$dpt_login_arr['login_area'] = $area;
			$dpt_login_arr['email'] = $profile['email'];
			$dpt_login_arr['password'] = $password;
			$dpt_login_arr['admin_user_id'] = '';
			$dpt_login_arr['url'] = $_SERVER['REQUEST_URI'];
			$dpt_account_id = $account_id;
			
			$model_client = new Model_UserClient();
			$result = $model_client->linkClientService($user_id, $dpt_id, $dpt_account_id, $dpt_login_arr);
			if ($result){
				$ss = new Zend_Session_Namespace('service');
				$ss->link_result = 'success';
				$ss->link_dpt_id = $dpt_id;
				$ss->link_login_account = $login_account;
				$ss->link_dpt_account_id = $account_id;
				
				return true;
			}else 
				return false;			
		}else{
			return false;
		}
		
	}
	
	
	public function getTimezone($timezone_id)
	{
		$select  = $this->db->select()->from('timezone')->where('timezone_id = ?', $timezone_id);
		$timezone = $this->db->fetchRow($select);
		if (!$timezone) {
			throw new Exception("Timezone [$timezone] does not exist!");
		}
		
		$timezone_name = $this->getCpxcenterTimezone( trim($timezone['timezone_hour']) );
		
		return $timezone_name;
	}	
		
	
	/**
	* get cpx center timezone names from timezone_hour
	*/
	public function getCpxcenterTimezone( $timezone_hour )
	{
		$tz = array();
		
		$tz['Kwajalein']	= '(GMT-12:00) International Date Line West';
		$tz['Pacific/Midway']	= '(GMT-11:00) Midway Island, Samoa';
		$tz['Pacific/Honolulu']	= '(GMT-10:00) Hawaii';
		$tz['America/Anchorage']	= '(GMT-09:00) Alaska';
		$tz['America/Los_Angeles']	= '(GMT-08:00) Pacific Time (US & Canada); Tijuan';
		$tz['America/Phoenix']	= '(GMT-07:00) Mountain Time (US & Canada)';
		$tz['America/Chicago']	= '(GMT-06:00) Central Time (US & Canada)';
		$tz['America/New_York']	= '(GMT-05:00) Eastern Time (US & Canada)';
		$tz['America/Santiago']	= '(GMT-04:00) Atlantic Time (Canada)';
		$tz['America/Sao_Paulo']	= '(GMT-03:00) Greenland';
		$tz['Atlantic/South_Georgia']	= '(GMT-02:00) Mid-Atlantic';
		$tz['Atlantic/Cape_Verde']	= '(GMT-01:00) Cape Verde Is.';
		$tz['Europe/Dublin']	= '(GMT) Dublin';
		$tz['Europe/Belgrade']	= '(GMT+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm';
		$tz['Europe/Vienna']	= '(GMT+02:00) Helsinki, Kyiv, Riga, Sofia, Tallinn';
		$tz['Europe/Moscow']	= '(GMT+03:00) Moscow, St. Petersburg, Volgograd, Baghdad';
		$tz['Asia/Muscat']	= '(GMT+04:00) Abu Dhabi, Muscat';
		$tz['Asia/Yekaterinburg']	= '(GMT+05:00) Ekaterinburg';
		$tz['Asia/Dhaka']	= '(GMT+06:00) Astana, Dhaka';
		$tz['Asia/Krasnoyarsk']	= '(GMT+07:00) Bangkok, Hanoi, Jakarta';
		$tz['Asia/Hong_Kong']	= '(GMT+08:00) Beijing, Chongqing, Hong Kong, Urumqi';
		$tz['Asia/Tokyo']	= '(GMT+09:00) Osaka, Sapporo, Tokyo';
		$tz['Australia/Sydney']	= '(GMT+10:00) Canberra, Melbourne, Sydney';
		$tz['Asia/Magadan']	= '(GMT+11:00) Magadan, Solomon Is., New Caledonia';
		$tz['Pacific/Fiji']	= '(GMT+12:00) Fiji, Kamchatka, Marshall Is.';
		$tz['Pacific/Tongatapu']	= "(GMT+13:00) Nuku'alofa";
			
		if ($timezone_hour=='GMT'){
			$tz_name = 'Europe/Dublin';
		}else{
			$found=false;
			foreach ($tz as $key => $value ){
				if ( strpos($value, $timezone_hour)!==false ){
					$tz_name = $key;
					$found=true;
					break;
				}
			}
			if (!$found)
				$tz_name = 'Europe/Dublin';
		}
		
		return $tz_name;		
		
	}
	
		
}